package com.media.controller;

import com.media.cache.CacheUtil;
import com.media.common.enums.PushType;
import com.media.common.reault.AjaxResult;
import com.media.common.reault.BaseController;
import com.media.pojo.CameraPojo;
import com.media.service.CameraService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 拉流的controller
 **/
@RestController
@RequestMapping("/camera")
@Slf4j
public class CameraController extends BaseController {

    @Resource
    CameraService cameraService;

    /**
     * 测试接口
     **/
    @GetMapping(value = "/test")
    public AjaxResult test() {
        return success();
    }

    /**
     * ip        摄像头iP
     * username  用户名字
     * password  用户密码
     * channel   通道
     * stream    码流
     * : 开启视频流
     **/
    @PostMapping(value = "/openCameraRtsp")
    public AjaxResult openCameraRtsp(@RequestBody CameraPojo cameraPojo) {
        return success(cameraService.openCameraRtsp(cameraPojo));
    }

    /**
     * ip        摄像头iP
     * username  用户名字
     * password  用户密码
     * channel   通道
     * stream    码流
     * : 开启视频流
     **/
    @PostMapping(value = "/openCameraRtmp")
    public AjaxResult openCameraRtmp(@RequestBody CameraPojo cameraPojo) {
        cameraPojo.setPushType(PushType.RTMP);
        return success(cameraService.openCamera(cameraPojo));
    }

    /**
     * ip        摄像头iP
     * username  用户名字
     * password  用户密码
     * channel   通道
     * stream    码流
     * : 开启视频流
     **/
    @PostMapping(value = "/openCameraHls")
    public AjaxResult openCameraHls(@RequestBody CameraPojo cameraPojo) {
        cameraPojo.setPushType(PushType.HLS);
        return success(cameraService.openCamera(cameraPojo));
    }

    /**
     * 录制视频
     * ip        摄像头iP
     * username  用户名字
     * password  用户密码
     * channel   通道
     * stream    码流
     * time      持续的秒(默认30)
     * path      视频保存的路径
     **/
    @PostMapping(value = "/recordVideo")
    public AjaxResult recordVideo(@RequestBody CameraPojo cameraPojo) {
        return success(cameraService.recordVideo(cameraPojo));
    }

    /**
     * 裁剪视频 (默认截取 recordVideo 这个接口录制视频的 第 20秒 ~ 30秒)
     **/
    @GetMapping(value = "/cutOutVideo")
    public AjaxResult cutOutVideo(
            @RequestParam("sourceUrl") String sourceUrl,
            @RequestParam("toUrl") String toUrl,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime
    ) {
        return success(cameraService.cutOutVideo(sourceUrl, toUrl, startTime, endTime));
    }

    /**
     * 拉取图片
     * ip        摄像头iP
     * username  用户名字
     * password  用户密码
     * channel   通道
     * stream    码流
     * path      图片保存的路径
     * 拉取一张图片
     **/
    @PostMapping(value = "/recordImage")
    public AjaxResult recordImage(@RequestBody CameraPojo cameraPojo) {
        return success(cameraService.recordImage(cameraPojo));
    }

    /**
     * 拉取图片(多张)
     * ip        摄像头iP
     * username  用户名字
     * password  用户密码
     * channel   通道
     * stream    码流
     * path      图片保存的路径
     * time      持续的秒(默认30)
     * (一秒一张)
     **/
    @PostMapping(value = "/recordContinueToImage")
    public AjaxResult recordContinueToImage(@RequestBody CameraPojo cameraPojo) {
        return success(cameraService.recordContinueToImage(cameraPojo));
    }


    /**
     * 关闭视频流 多个流用 , 分割
     *
     * @param tokens token
     **/
    @DeleteMapping(value = "/cameras/{tokens}")
    public AjaxResult closeCamera(@PathVariable("tokens") String tokens) {
        cameraService.closeCamera(tokens);
        return success();
    }

    /**
     * 获取到所有的视频流
     **/
    @GetMapping(value = "/cameras")
    public AjaxResult getCameras() {
        return success(CacheUtil.STREAM_MAP.values());
    }

    /**
     * 视频流保活
     **/
    @PutMapping(value = "/cameras/{tokens}")
    public AjaxResult keepAlive(@PathVariable("tokens") String tokens) {
        cameraService.keepAlive(tokens);
        return success();
    }

    /**
     * 获取服务信息
     **/
    @GetMapping(value = "/status")
    public AjaxResult getConfig() {
        return success(cameraService.getConfig());
    }
}
